{
 "nbformat": 4,
 "nbformat_minor": 2,
 "metadata": {
  "language_info": {
   "name": "python",
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "version": "3.7.5"
  },
  "orig_nbformat": 2,
  "file_extension": ".py",
  "mimetype": "text/x-python",
  "name": "python",
  "npconvert_exporter": "python",
  "pygments_lexer": "ipython3",
  "version": 3
 },
 "cells": [
  {
   "cell_type": "markdown",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 26. 删除排序数组中的重复项"
   ]
  },
  {
   "cell_type": "markdown",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "给定一个排序数组，你需要在原地删除重复出现的元素，使得每个元素只出现一次，返回移除后数组的新长度。\n",
    "\n",
    "不要使用额外的数组空间，你必须在原地修改输入数组并在使用 O(1) 额外空间的条件下完成。\n",
    "\n",
    "示例 1:\n",
    "\n",
    "给定数组 nums = [1,1,2], \n",
    "\n",
    "函数应该返回新的长度 2, 并且原数组 nums 的前两个元素被修改为 1, 2。 \n",
    "\n",
    "你不需要考虑数组中超出新长度后面的元素。\n",
    "示例 2:\n",
    "\n",
    "给定 nums = [0,0,1,1,1,2,2,3,3,4],\n",
    "\n",
    "函数应该返回新的长度 5, 并且原数组 nums 的前五个元素被修改为 0, 1, 2, 3, 4。\n",
    "\n",
    "你不需要考虑数组中超出新长度后面的元素。\n",
    "说明:\n",
    "\n",
    "为什么返回数值是整数，但输出的答案是数组呢?\n",
    "\n",
    "请注意，输入数组是以“引用”方式传递的，这意味着在函数里修改输入数组对于调用者是可见的。\n",
    "\n",
    "你可以想象内部操作如下:\n",
    "\n",
    "// nums 是以“引用”方式传递的。也就是说，不对实参做任何拷贝\n",
    "int len = removeDuplicates(nums);\n",
    "\n",
    "// 在函数里修改输入数组对于调用者是可见的。\n",
    "// 根据你的函数返回的长度, 它会打印出数组中该长度范围内的所有元素。\n",
    "for (int i = 0; i < len; i++) {\n",
    "    print(nums[i]);\n",
    "}\n",
    "\n",
    "来源：力扣（LeetCode）\n",
    "链接：https://leetcode-cn.com/problems/remove-duplicates-from-sorted-array"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": "1\n"
    }
   ],
   "source": [
    "class Solution(object):\n",
    "    def removeDuplicates(self, nums):\n",
    "        \"\"\"\n",
    "        :type nums: List[int]\n",
    "        :rtype: int\n",
    "        \"\"\"\n",
    "        if len(nums) == 0:\n",
    "            return 0\n",
    "        i = 0\n",
    "        for j in range(1,len(nums)):\n",
    "            if nums[j] != nums[i]:\n",
    "                i += i\n",
    "                nums[i] = nums[j]\n",
    "        return i + 1\n",
    "\n",
    "if __name__ == \"__main__\":\n",
    "    Sol = Solution()\n",
    "    end = Sol.removeDuplicates([1,2,3,4,4,5])\n",
    "    print(end)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": "5\n"
    }
   ],
   "source": [
    "class Solution(object):\n",
    "    def removeDuplicates(self, nums):\n",
    "        \"\"\"\n",
    "        :type nums: List[int]\n",
    "        :rtype: int\n",
    "        \"\"\"\n",
    "        length=len(nums)\n",
    "        count=0\n",
    "        for i in range(1,length):\n",
    "            if nums[count]!=nums[i]:\n",
    "                nums[count+1]=nums[i]\n",
    "                count+=1\n",
    "        return count+1\n",
    "\n",
    "if __name__ == \"__main__\":\n",
    "    Sol = Solution()\n",
    "    end = Sol.removeDuplicates([1,2,3,4,4,5])\n",
    "    print(end)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ]
}